package com.yfqy.app.modules.address.resource;

import com.yfqy.app.domain.vo.ResultVO;
import com.yfqy.app.modules.address.dto.AddressOperateDTO;
import com.yfqy.app.modules.address.dto.AreaQueryDTO;
import com.yfqy.app.modules.address.dto.DeliveryAddressDTO;
import com.yfqy.app.modules.address.service.AddressService;
import com.yfqy.app.modules.address.vo.AreaVO;
import com.yfqy.app.modules.address.vo.DeliveryAddressVO;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * customer的地址管理
 */
@RestController
@RequestMapping("/api/client/address")
@RequiredArgsConstructor
public class AddressController {


    private final AddressService addressService;

    /**
     * customer 新建收货地址
     */
    @PostMapping("/add")
    public ResultVO<Long> addAddress(@Valid @RequestBody DeliveryAddressDTO addressDTO) {
        Long addressId = addressService.addAddress(addressDTO);
        return ResultVO.data(addressId);
    }

    /**
     * customer修改收货地址
     */
    @PostMapping("/update/{id}")
    public ResultVO<Void> updateAddress(@PathVariable Long id,
                                        @Valid @RequestBody DeliveryAddressDTO addressDTO) {
        addressService.updateAddress(id, addressDTO);
        return ResultVO.success();
    }

    /**
     * customer删除收获地址
     */
    @PostMapping("/delete/{id}")
    public ResultVO<Void> deleteAddress(@PathVariable Long id) {
        addressService.deleteAddress(id);
        return ResultVO.success();
    }

    /**
     * customer的收获地址详情
     */
    @GetMapping("/detail/{id}")
    public ResultVO<DeliveryAddressVO> getAddressDetail(@PathVariable Long id) {
        DeliveryAddressVO address = addressService.getAddressDetail(id);
        return ResultVO.data(address);
    }

    /**
     * customer的全部收获地址
     */
    @GetMapping("/customerAddress")
    public ResultVO<List<DeliveryAddressVO>> customerAddress() {
        List<DeliveryAddressVO> addresses = addressService.customerAddress();
        return ResultVO.data(addresses);
    }

    /**
     * customer设置默认收货地址
     */
    @PostMapping("/setDefault")
    public ResultVO<Void> setDefaultAddress(@Valid @RequestBody AddressOperateDTO dto) {
        addressService.setDefaultAddress(dto.getId());
        return ResultVO.success();
    }

    /**
     * 查询省、市、区/县
     *
     * @param queryDTO
     * @return
     */
    @PostMapping("/areas")
    public ResultVO<List<AreaVO>> areas(@Valid @RequestBody AreaQueryDTO queryDTO) {
        return ResultVO.data(addressService.areas(queryDTO));
    }
    
}
